'''
@Author: 冯浩
@Date: 2019-12-09 23:01:40
@LastEditors: 冯浩
@LastEditTime: 2019-12-11 22:31:43
@FilePath: \bob_develop\fluent_python\示例5_21.py
'''
# 函数式编程
from operator import mul
from functools import reduce
'''
>>>def add(x, y) :            # 两数相加
...     return x + y
... 
>>> reduce(add, [1,2,3,4,5])   # 计算列表和：1+2+3+4+5
15
>>> reduce(lambda x, y: x+y, [1,2,3,4,5])  # 使用 lambda 匿名函数
15
'''
# reduce() 函数会对参数序列中元素进行累积。


def multiplication(a, b) -> int:
    # 乘法函数
    return a*b


def fact(n: int) -> int:
    return reduce(multiplication, range(1, n+1))


print(fact(10))


def fact_2(n: int) -> int:
    return reduce(mul, range(1, n+1))

print(fact_2(10))
